"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : ALTER PROCEDURE
Case Name   : 无存储过程alter权限,非存储过程所有者普通用户修改存储过程名称
Create At   : 2022/05/10
Owner       : @zou_jialiang050
Description :
    1.创建用户1
    2.创建用户2
    3.用户1创建procedure
    4.用户2修改存储过程名称
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.合理报错
    5.成功
History     :
"""

import os
import unittest
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class PROCEDURE(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Con = Constant()
        self.user1 = 'u_alter_procedure_0006_01'
        self.user2 = 'u_alter_procedure_0006_02'
        self.table = 't_alter_procedure_0006'
        self.procedure = 'p_alter_procedure_0006_01'
        self.procedure1 = 'p_alter_procedure_0006_02'

    def test_alter_procedure(self):
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        step = 'step1:创建用户1 expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            drop user if exists {self.user1} cascade;
            create user {self.user1} with password '{macro.PASSWD_REPLACE}';''')
        self.logger.info(create_user)
        self.assertIn(self.Con.CREATE_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step2:创建用户2  expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            drop user if exists {self.user2} cascade;
            create user {self.user2} with password '{macro.PASSWD_REPLACE}';
            ''')
        self.logger.info(create_user)
        self.assertIn(self.Con.CREATE_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step3:用户1创建procedure expect:成功'
        self.logger.info(step)
        create_pro = self.primary_sh.execut_db_sql(
            f'''create or replace procedure {self.procedure}()
            is
            var1 int:= 1;
            var2 int:= 2;
            begin
            drop table if exists {self.table};
            create table {self.table}(c_int int);
            insert into {self.table} values(var1),(var2);
            end;''',
            sql_type=f" -U {self.user1} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(create_pro)
        self.assertIn(self.Con.CREATE_PROCEDURE_SUCCESS_MSG, create_pro,
                      '执行失败' + step)

        step = 'step4:用户2修改存储过程名称 expect:合理报错'
        self.logger.info(step)
        alter_procedure = self.primary_sh.execut_db_sql(
            f'''set current_schema = {self.user1};
            alter procedure {self.procedure}() rename to {self.procedure1};
            ''', sql_type=f" -U {self.user2} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(alter_procedure)
        self.assertIn(f'ERROR:  function {self.procedure}() does not exist',
                      alter_procedure, '执行失败' + step)

    def tearDown(self):
        step = 'step5:清理环境 expect:清理环境成功'
        self.logger.info(step)
        clean_environment = self.primary_sh.execut_db_sql(
            f'''drop procedure {self.procedure};''',
            sql_type=f" -U {self.user1} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(clean_environment)
        clean_environment1 = self.primary_sh.execut_db_sql(
            f'''drop user {self.user1} cascade;
            drop user {self.user2} cascade;''')
        self.logger.info(clean_environment1)
        self.assertIn(self.Con.DROP_PROCEDURE_SUCCESS_MSG, clean_environment,
                      '执行失败' + step)
        self.assertEqual(clean_environment1.count
                         (self.Con.DROP_ROLE_SUCCESS_MSG), 2,
                         '执行失败' + step)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
